using Common.Logging;
using System;
using System.Collections.Generic;
namespace AppToolKit.Business.Observer
{
	public class ObserverManager : IObserverManager
	{
		private static ILog logger_ = LogManager.GetLogger(typeof(ObserverManager));
		private Dictionary<string, Dictionary<string, EventHook>> subjectes_;
		public List<object> RegisterObjects
		{
			set
			{
				this.Load(value);
			}
		}
		private void Load(List<object> registerObjects)
		{
			foreach (object current in registerObjects)
			{
				if (current is ISubject)
				{
					((ISubject)current).ObserveMgr = this;
				}
				if (current is IObserver)
				{
					((IObserver)current).Register(this);
				}
			}
		}
		public void Register(string subject, string observerEvent, ObserveEventCallbackTemplate callback)
		{
			if (this.subjectes_ == null)
			{
				this.subjectes_ = new Dictionary<string, Dictionary<string, EventHook>>();
			}
			if (!this.subjectes_.ContainsKey(subject))
			{
				this.subjectes_[subject] = new Dictionary<string, EventHook>();
			}
			if (!this.subjectes_[subject].ContainsKey(observerEvent))
			{
				this.subjectes_[subject][observerEvent] = new EventHook();
			}
			EventHook expr_7E = this.subjectes_[subject][observerEvent];
			expr_7E.PlaceHolder = (EventHandler<ObserveEventCallbackArgs>)Delegate.Combine(expr_7E.PlaceHolder, callback.Process);
		}
		public void Register(Type type, string observerEvent, ObserveEventCallbackTemplate callback)
		{
			this.Register(type.FullName, observerEvent, callback);
		}
		public string ConvertToName<T>(Enum enumEvent)
		{
			return Enum.GetName(typeof(T), enumEvent);
		}
		public void PublishEvent(object sender, string subject, string eventName, ObserveEventCallbackArgs args)
		{
			if (this.subjectes_ != null && this.subjectes_.ContainsKey(subject))
			{
				if (this.subjectes_[subject].ContainsKey(eventName))
				{
					if (this.subjectes_[subject][eventName].PlaceHolder != null)
					{
						args.Event = eventName;
						this.subjectes_[subject][eventName].Execute(sender, args);
					}
				}
			}
		}
	}
}
